package util;

/**
 * Created by Administrator on 2017/7/12.
 */

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class FileUtils {

    /**
     * 根目录
     */
    public static final String ROOT_DIR = Environment.getExternalStorageDirectory() + "/ci_helper";

    /**
     * 背景目录
     */
    public static final String BACKGROUND_DIR = ROOT_DIR + "/background";

    /**
     * 成果目录
     */
    public static final String WORK_DIR = ROOT_DIR + "/work";

    /**
     * 保存文件
     *
     * @param bm
     * @param fileName
     * @throws IOException
     */
    public static String saveFile(Bitmap bm) throws IOException {
        createDir(ROOT_DIR);
        createDir(BACKGROUND_DIR);
        String fileName = bm.hashCode() + "";
        File myCaptureFile = new File(BACKGROUND_DIR + "/" + fileName + ".jpg");
        if (!myCaptureFile.exists()) {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
        }
        return myCaptureFile.getAbsolutePath();

    }

    private static void createDir(String dir) {
        File root = new File(dir);
        if (!root.exists()) {
            root.mkdir();
        }
    }

    /**
     * 保存文件
     *
     * @param bm
     * @param fileName
     * @throws IOException
     */
    public static String saveFile(Bitmap bm, String fileName) throws IOException {
        createDir(ROOT_DIR);
        createDir(WORK_DIR);
        File myCaptureFile = new File(WORK_DIR + "/" + fileName + ".jpg");
        if (!myCaptureFile.exists()) {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
        }
        return myCaptureFile.getAbsolutePath();
    }

    public static byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }


    public static void writeTxt(String filePath, String fileName, String message) {

        //创建目录
        File newPath = new File(filePath);
        if (!newPath.exists()) {
            newPath.mkdir();
        }
        //String absPath = Constants.File_PATH + "/work" + "/fuck.txt";
        //File newfile = new File(Constants.File_PATH + "/work",fileName);
        //createDir(absPath);

        //创建文件
        File newFile = new File(filePath + "/" + fileName);
        if (!newFile.exists()) {
            try {
                newFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        try {

            // FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);

            FileOutputStream fout = new FileOutputStream(newFile);

            byte[] bytes = message.getBytes();

            fout.write(bytes);

            fout.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static Intent getTextFileIntent(String param, boolean paramBoolean) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (paramBoolean) {
            Uri uri1 = Uri.parse(param);
            intent.setDataAndType(uri1, "text/plain");
        } else {
            Uri uri2 = Uri.fromFile(new File(param));
            intent.setDataAndType(uri2, "text/plain");
        }
        return intent;
    }

    public static String DerrickWannaReadFileAAA(String path) {
        StringBuffer sb = new StringBuffer();
        File file = new File(path);
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = "";
            try {
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
            } catch (IOException e) {
            }//hhh
        } catch (FileNotFoundException e) {//hhh}

        }
        return sb.toString();
    }

    public static String pathTopath(String path) {
        String[] sa = path.split("work");
        path = sa[0] + "work/" + sa[1];
        return path;
    }
}
